#!/bin/bash

# Pre-commit hook for Claude Code plugin
# 1. Formats Lua files with StyLua
# 2. Runs linting with luacheck
# 3. Runs tests to ensure code quality

# ======== Style Formatting ========
# Check if stylua is installed
if ! command -v stylua &> /dev/null; then
  echo "Error: stylua is not installed. Please install it to format Lua code."
  echo "You can install it from: https://github.com/JohnnyMorganz/StyLua"
  exit 1
fi

# Check for luacheck
if ! command -v luacheck &> /dev/null; then
  echo "Warning: luacheck is not installed. Skipping lint checks."
  echo "You can install it using luarocks: luarocks install luacheck"
  HAS_LUACHECK=0
else
  HAS_LUACHECK=1
fi

# Get all staged Lua files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.lua$')

if [ -n "$STAGED_FILES" ]; then
  echo "Running StyLua on staged Lua files..."
  
  # Format all staged Lua files
  for FILE in $STAGED_FILES; do
    stylua "$FILE"
    git add "$FILE"
  done
  
  echo "Lua files have been formatted and staged."
  
  # Run luacheck if available
  if [ "$HAS_LUACHECK" -eq 1 ]; then
    echo "Running luacheck on staged Lua files..."
    LINT_ISSUES=0
    FIXABLE_ISSUES=0
    FIXED_FILES=""
    
    # First check for issues that we can automatically fix
    for FILE in $STAGED_FILES; do
      # Auto-fix trailing whitespace
      if grep -q "[[:space:]]$" "$FILE"; then
        sed -i 's/[[:space:]]*$//' "$FILE"
        FIXABLE_ISSUES=1
        FIXED_FILES="$FIXED_FILES $FILE"
      fi
      
      # Auto-fix line endings (ensure LF)
      if file "$FILE" | grep -q "CRLF"; then
        dos2unix "$FILE" 2>/dev/null
        FIXABLE_ISSUES=1
        FIXED_FILES="$FIXED_FILES $FILE"
      fi
    done
    
    # If we fixed any issues, add them back to staging
    if [ "$FIXABLE_ISSUES" -eq 1 ]; then
      echo "Fixed some linting issues automatically in:$FIXED_FILES"
      for FILE in $FIXED_FILES; do
        git add "$FILE"
      done
    fi
    
    # Now run the full luacheck to see if there are remaining issues
    for FILE in $STAGED_FILES; do
      luacheck "$FILE"
      if [ $? -ne 0 ]; then
        LINT_ISSUES=1
      fi
    done
    
    if [ "$LINT_ISSUES" -eq 1 ]; then
      echo "Error: Lua lint issues found that couldn't be fixed automatically."
      echo "Please fix the remaining issues before committing."
      echo "You can bypass this check with git commit --no-verify"
      exit 1
    fi
  fi
  
  # ======== Run Tests ========
  echo "Running tests to ensure code quality..."
  
  # Find the project root directory (where .git is)
  PROJECT_ROOT=$(git rev-parse --show-toplevel)
  
  # Change to the project root
  cd "$PROJECT_ROOT" || exit 1
  
  # Run the tests directly using a specific path
  FULL_PATH="/home/gregg/Projects/neovim/plugins/claude-code/scripts/test.sh"
  echo "Running test script at: $FULL_PATH"
  bash "$FULL_PATH"
  
  # Check if tests passed
  if [ $? -ne 0 ]; then
    echo "❌ Tests failed! Commit aborted."
    echo "Please fix the test failures before committing."
    exit 1
  fi
  
  echo "✅ All tests passed. Proceeding with commit."
fi

exit 0